﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace AuthorizedNavigation.Client.Services
{
    public abstract class DialogServiceBase : IDialogService
    {


        protected DialogServiceBase()
        {

        }


        public abstract IDialogWindow View
        {
            get;
            set;
        }

        public void ShowDialog()
        {
            if (this.View != null)
            {
                View.Show();
            }
        }

        public void CloseDialog()
        {
            if (this.View != null)
            {
                View.Close();
            }
        }

        public void ShowDialog(IDialogWindow view)
        {
            View.Show();
        }


        public void ShowDialog(IDialogWindow view, Action onDialogClose)
        {
            if (onDialogClose != null)
            {
                View.Closed += (send, args) => onDialogClose();
            }

            View.Show();

        }

        public void ShowDialog(IDialogWindow view, Action onDialogCloseOk, Action onDialogCloseCancel)
        {
            if ((onDialogCloseOk != null) || (onDialogCloseCancel != null))
            {
                View.Closed += (send, args) =>
                {

                    if ((View.DialogResult == true) && (onDialogCloseOk != null))
                    {
                        onDialogCloseOk();
                    }
                    else if ((View.DialogResult == false) && (onDialogCloseCancel != null))
                    {
                        onDialogCloseCancel();
                    }

                };
            }

            View.Show();
        }


        public void ShowDialog<ViewModelBase>(Action<ViewModelBase> onDialogCloseOk, Action<ViewModelBase> onDialogCloseCancel)
        {
            var viewModel = (ViewModelBase)View.DataContext;

            if ((onDialogCloseOk != null) || (onDialogCloseCancel != null))
            {
                EventHandler handler = null;
                handler = (send, args) =>
                {

                    if ((View.DialogResult == true) && (onDialogCloseOk != null))
                    {
                        onDialogCloseOk(viewModel);
                        View.Closed -= handler;
                    }
                    else if ((View.DialogResult == false) && (onDialogCloseCancel != null))
                    {
                        onDialogCloseCancel(viewModel);
                        View.Closed -= handler;
                    }

                };
                View.Closed += handler;
            }

            View.Show();

        }



    }
}
